//
//  ConfirmationViewController.m
//  PlayBall
//
//  Created by superman on 14-8-21.
//
//

#import "ConfirmationViewController.h"
#import "JddDataParser.h"
#import "JddViewControllerManager.h"
#import "JddChangePwdViewController.h"
#import "UIFitButton.h"
@interface ConfirmationViewController ()
{
    NSInteger secondsCountDown;
    NSTimer *timer;
}

@end

@implementation ConfirmationViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [MobClick beginLogPageView:@"ZhaoHuiMiMa"];
    
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"ZhaoHuiMiMa"];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.userNameLabel.text = _userName;
    self.phoneNumLabel.text = _phoneNum;
    self.confirmTextField.delegate = self;
    self.confirmTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.confirmTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    
    self.view.backgroundColor = [UIColor whiteColor];
    UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 30)];
    customLab.backgroundColor = [UIColor clearColor];
    [customLab setText:@"输入验证码"];
    customLab.textAlignment = NSTextAlignmentCenter;
    [customLab setTextColor:[UIColor whiteColor]];
    customLab.font = [UIFont systemFontOfSize:19];
    self.navigationItem.titleView = customLab;
//    //self.navigationItem.hidesBackButton = YES;
    
//    UIFitButton *backButton = [UIFitButton buttonWithType:UIButtonTypeCustom];
//    backButton.isleftBtn = YES;
//    backButton.frame = CGRectMake(0.0, 0.0, 12, 24);
//    [backButton setImage:[UIImage imageNamed:@"navbar_back"] forState:UIControlStateNormal];
//    [backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
//    UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
//    temporaryBarButtonItem.style = UIBarButtonItemStylePlain;
//    self.navigationItem.leftBarButtonItem = temporaryBarButtonItem;
    
    secondsCountDown = 60;
    timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector: @selector(timeCountDown) userInfo:nil repeats:YES];
    
    //添加手势，点击任意处隐藏键盘
    UITapGestureRecognizer *oneTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyBoard)];
    oneTap.delegate = self;
    oneTap.numberOfTouchesRequired = 1;
    [self.view addGestureRecognizer:oneTap];
    
}
//计时器，60秒倒计时
- (void)timeCountDown
{
    self.timeLabel.text = [NSString stringWithFormat:@"%d",secondsCountDown];
    secondsCountDown --;
    if (secondsCountDown == -1) {
        self.ageinBtn.hidden = NO;
       // [timer invalidate];
        [timer setFireDate:[NSDate distantFuture]];
        self.timeLabel.hidden = YES;
        self.noticeLabel.hidden = YES;
    }
}
//点击任意处隐藏键盘
- (void)hideKeyBoard{
    [self.confirmTextField resignFirstResponder];
}

- (void)backAction
{
    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:2] animated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)againSendBtnClicked:(id)sender {
    [JddCommonFunction setWaitingViewHidden:NO fromController:self];
    JddLookForPasswordOperation *operation = [[JddLookForPasswordOperation alloc] init];
    operation.delegate = self;
    operation.userName = self.userNameLabel.text;
    operation.userPhoneNum = self.phoneNumLabel.text;
    [[JddGlobalParameter sharedInstance].operationQueue addOperation:operation];
}
//重新发送验证码的回调函数
- (void)FindPasswordOperationResult:(NSData *)result
{
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:result options:NSJSONReadingMutableContainers error:nil];
    [JddCommonFunction setWaitingViewHidden:YES fromController:self];
    if (result) {
        NSString *uId = [JddDataParser parseLoginResult:result];
        if (uId.length == 0) {
            [JddCommonFunction messageBoxWithMessage:[JddError sharedInstance].message];
        }
        if ([[dic objectForKey:@"Code"]intValue] == 0) {
            //发送成功后，重新出现下面的倒计时
            self.timeLabel.hidden = NO;
            self.noticeLabel.hidden = NO;
            self.ageinBtn.hidden = YES;
            secondsCountDown = 60;
            [timer setFireDate:[NSDate distantPast]];
        }
    }
}


- (IBAction)nextBtnClicked:(id)sender {
    
    if ([_confirmTextField.text isEqualToString:@""]) {
        [JddCommonFunction messageBoxWithMessage:@"请输入验证码"];
    }else{
        JddSimmitSecuritycode *operation = [[JddSimmitSecuritycode alloc] init];
        operation.delegate = self;
        operation.verifyCode = self.confirmTextField.text;
        operation.userName = self.userNameLabel.text;
        [[JddGlobalParameter sharedInstance].operationQueue addOperation:operation];
    }
}
//提交验证码的回调函数 
- (void)SimmitSecuritycodeResult:(NSData *)result
{
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:result options:NSJSONReadingMutableContainers error:nil];
    if (result) {
        NSString *uId = [JddDataParser parseLoginResult:result];
        if (uId.length == 0) {
            [JddCommonFunction messageBoxWithMessage:[JddError sharedInstance].message];

        }
        if ([[dic objectForKey:@"Code"]intValue] == 0) {
        
            JddChangePwdViewController *changePwdView = [[JddChangePwdViewController alloc] init];
            changePwdView.userName = self.userName;
            changePwdView.phoneNum = self.phoneNum;
            [self.navigationController pushViewController:changePwdView animated:YES];
        }
        
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.confirmTextField resignFirstResponder];
    return YES;
}

- (BOOL)textFieldShouldClear:(UITextField *)textField{
    return  YES;
}
- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:YES];
}

@end
